import re
import pymysql

from django import http
from django.contrib.auth import authenticate, login
from django.db import DatabaseError
from django.http import JsonResponse
from django.shortcuts import render, redirect

# Create your views here.
from django.urls import reverse
from django.views import View

from apps.users.models import User


class RegisterVies(View):
    def get(self,request):
        return render(request,'register.html')
    def post(self,request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        password2 = request.POST.get('password2')
        mobile = request.POST.get('mobile')
        allow = request.POST.get('allow')
        # 判断参数是否齐全
        if not all([username,password,password2,mobile,allow]):
            return http.HttpResponseBadRequest('缺少必传参数')
        # 判断用户名是否是5-20个字符
        if not re.match(r'^[a-zA-Z0-9_]{5,20}$',username):
            return http.HttpResponseBadRequest('请输入5-20个字符的用户名')
        # 判断密码是否是8-20个数字
        if not re.match(r'^[a-zA-Z0-9]{8,20}$',password):
            return http.HttpResponseBadRequest('请输入8-20位的密码')
        if password != password2:
            return http.HttpResponseBadRequest('两次输入的密码不一致')
        if not re.match(r'^1[3-9]\d{9}$',mobile):
            return http.HttpResponseBadRequest('请输入正确的手机号码')
        if allow != 'on':
            return http.HttpResponseBadRequest('请勾选用户协议')

        try:
            user=User.objects.create_user(username=username,password=password,
                                     mobile=mobile)
            from django.contrib.auth import login
            login(request,user)
        except DatabaseError:
            return render(request,'register.html',{'register_errmsg':'注册失败'})
        # return http.HttpResponse('注册成功,重定向到首页')
        return redirect(reverse('contents:index'))

class Repeat(View):
    def get(self,request,username):
        count = User.objects.filter(username=username).count()
        return http.JsonResponse({'count':count})
class RepeatNum(View):
    def get(self,request,usernum):
        count = User.objects.filter(mobile=usernum).count()
        return http.JsonResponse({'count':count})
class Sign(View):
    def get(self,request):
        return render(request,'login.html')
    def post(self,request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        remembered = request.POST.get('remembered')
        if not all([username,password]):
            return http.HttpResponseBadRequest('缺少必传参数')
        if not re.match(r'^[a-zA-Z0-9_-]{5,20}$', username):
            return http.HttpResponseBadRequest('帐号必须是5-20位')
        if not re.match(r'^[0-9A-Za-z]{8,20}$', password):
            return http.HttpResponseBadRequest('密码必须是8-20位')
        user = authenticate(username=username,password=password)
        if not user:
            return http.HttpResponseBadRequest('帐号密码错误')
        login(request,user)
        if remembered != 'on':
            request.session.set_expiry(0)
        else:
            request.session.set_expiry(None)
        return redirect(reverse('contents:index'))








